package org.jivesoftware.smack;

import org.apache.harmony.javax.security.auth.callback.Callback;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.auth.callback.PasswordCallback;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.Authentication;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;

class NonSASLAuthentication
  implements UserAuthentication
{
  private Connection connection;

  public NonSASLAuthentication(Connection paramConnection)
  {
    this.connection = paramConnection;
  }

  public String authenticate(String paramString1, String paramString2, String paramString3)
    throws XMPPException
  {
    PacketCollector localPacketCollector2;
    IQ localIQ2;
    Authentication localAuthentication1 = new Authentication();
    localAuthentication1.setType(IQ.Type.GET);
    localAuthentication1.setUsername(paramString1);
    PacketCollector localPacketCollector1 = this.connection.createPacketCollector(new PacketIDFilter(localAuthentication1.getPacketID()));
    this.connection.sendPacket(localAuthentication1);
    IQ localIQ1 = (IQ)localPacketCollector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (localIQ1 == null)
      throw new XMPPException("No response from the server.");
    if (localIQ1.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ1.getError());
    Authentication localAuthentication2 = (Authentication)localIQ1;
    localPacketCollector1.cancel();
    Authentication localAuthentication3 = new Authentication();
    localAuthentication3.setUsername(paramString1);
    if (localAuthentication2.getDigest() != null)
      localAuthentication3.setDigest(this.connection.getConnectionID(), paramString2);
    while (true)
    {
      localAuthentication3.setResource(paramString3);
      localPacketCollector2 = this.connection.createPacketCollector(new PacketIDFilter(localAuthentication3.getPacketID()));
      this.connection.sendPacket(localAuthentication3);
      localIQ2 = (IQ)localPacketCollector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
      if (localIQ2 != null)
        break label246;
      throw new XMPPException("Authentication failed.");
      if (localAuthentication2.getPassword() == null)
        break;
      localAuthentication3.setPassword(paramString2);
    }
    throw new XMPPException("Server does not support compatible authentication mechanism.");
    if (localIQ2.getType() == IQ.Type.ERROR)
      label246: throw new XMPPException(localIQ2.getError());
    localPacketCollector2.cancel();
    return localIQ2.getTo();
  }

  public String authenticate(String paramString1, String paramString2, CallbackHandler paramCallbackHandler)
    throws XMPPException
  {
    PasswordCallback localPasswordCallback = new PasswordCallback("Password: ", false);
    try
    {
      Callback[] arrayOfCallback = new Callback[1];
      arrayOfCallback[0] = localPasswordCallback;
      paramCallbackHandler.handle(arrayOfCallback);
      String str = authenticate(paramString1, String.valueOf(localPasswordCallback.getPassword()), paramString2);
      return str;
    }
    catch (Exception localException)
    {
      throw new XMPPException("Unable to determine password.", localException);
    }
  }

  public String authenticateAnonymously()
    throws XMPPException
  {
    String str;
    Authentication localAuthentication = new Authentication();
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(localAuthentication.getPacketID()));
    this.connection.sendPacket(localAuthentication);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    if (localIQ == null)
      throw new XMPPException("Anonymous login failed.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
    localPacketCollector.cancel();
    if (localIQ.getTo() != null)
      str = localIQ.getTo();
    while (true)
    {
      return str;
      str = this.connection.getServiceName() + "/" + ((Authentication)localIQ).getResource();
    }
  }
}